<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\Role as MyRole;

class Role extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public $role = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->role = new MyRole();
    }

    public function index()
    {
        $list = Db::table('lamp_role')->order('id', 'ASC')->select();
//         var_dump($list);


        return view('admin@con2/func1', [
            'list' => $list
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('admin@con2/func2');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p = $request->post();
        //判断输入的说明和职位是否为空
        if (empty($p['name']) || empty($p['remark'])) {
            return $this->error('不能留空!');

        }
//

        $role = new MyRole();
        $role->data($p);

        $result = $role->allowField(true)->save();
        if ($result > 0) {
            return $this->success('添加成功', url('admin/Role/index'));
        } else {
            return $this->error('添加失败!');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $list = $this->role->find($id);
//        var_dump($list);
        return view('admin@con2/func3', [
            'list' => $list
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $p = $request->post();
//        var_dump($p);

        $res = $this->role->allowField(true)->save($p, ['id' => $p['id']]);
        if ($res > 0) {
            return $this->success('更新成功!', url('admin/Role/index'));
        } else {
            return $this->error('更新失败!');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $roles = new MyRole();
        $res = $roles->where(['id' => $id])->delete();
        //同时准备删掉lamp_role_node 表中数据
//            Db::table('lamp_user_role')->where(['rid'=>$id])->delete();

        if ($res > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除失败!';
        }

        return json($info);
    }

    public function readch($id)
    {
        $res = Db::table(['lamp_role_node' => 'lrn', 'lamp_node' => 'ln'])->field(['ln.name'])->where('lrn.rid = ' . $id . ' and lrn.nid = ln.id')->select();
//        var_dump($res);
        return view('admin@con2/func4', ['list' => $res]);

    }

    public function roleList($id)
    {

        $name = Db::table('lamp_role')->field('name')->find($id);
//        var_dump($id);die;
        //cha查询对应的id所拥有的权限
        $old=Db::table(['lamp_role_node'=>'lrn','lamp_node'=>'ln'])->field(['ln.id'])->where('lrn.rid = '.$id.' and lrn.nid = ln.id')->select();
//        var_dump($old);die;
        $arr=array();
        foreach ($old as $v)
        {
            $arr[]=$v['id'];
        }
//        var_dump($arr);die;
//        $arr=array();
        //如何分辨相同名字的权限
        $node=Db::table('lamp_node')->field(['id','name'])->order('id','ASC')->select();
//        var_dump($node);die;



//.
        return view('admin@con2/func5',['id' =>$id,
            'name' => $name,
            'arr'  => $arr,
            'list' => $node]);
    }

    public function saveRole()
    {
//        var_dump($_POST);
        $rid = $_POST['rid'];

        Db::table('lamp_role_node')->where('rid='.$rid)->delete();
        //删完开始添加
//        var_dump($_POST['role']);die;
        foreach($_POST['role'] as $v)
        {
            $data['rid'] = $rid;
            $data['nid'] = $v;
            Db::table('lamp_role_node')->insert($data);

        }

        $this->success('权限分配成功', url('admin/Role/index'));


    }
}
   